//
//  SearchSectionHeaderCollectionReusableView.swift
//  APP
//
//  Created by gy on 2023/8/8.
//

import UIKit

class SearchSectionHeaderCollectionReusableView: UICollectionReusableView {

    private var item = SearchSectionItem()
    var clickHolder: ((String)->Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        DispatchQueue.main.async {
            self.lineView.addGradientLayer(gradientColors:  [UIColor(red: 1, green: 0.839, blue: 0, alpha: 1), UIColor(red: 0.851, green: 0.851, blue: 0.851, alpha: 0)], gradientDirection: .horizontally)
        }
    }
    static func getSectionHeight() -> CGFloat {
        return 40
    }
    func setInfo(sectionItem: SearchSectionItem) {
        self.item = sectionItem
        titleLabel.text = sectionItem.title
        rightBtn.isHidden = sectionItem.iconName.count == 0
        rightBtn.setImage(sectionItem.iconName.count > 0 ? UIImage(named: sectionItem.iconName): UIImage(), for: .normal)
    }
    
    // MARK: - UIButtonAction
    @objc func rightBtnAction() {
        guard let holder = clickHolder else { return }
        return holder(item.title)
    }
    
    // MARK: - configUI
    func configUI() {
        
        addSubview(backView)
        backView.addSubview(rightBtn)
        backView.addSubview(titleBackView)
        titleBackView.addSubview(titleLabel)
        titleBackView.addSubview(lineView)
        
        backView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.top.right.bottom.equalToSuperview()
        }
        rightBtn.snp.makeConstraints { make in
            make.right.top.bottom.equalToSuperview()
            make.width.equalTo(16*2+18)
        }
        titleBackView.snp.makeConstraints { make in
            make.left.centerY.equalToSuperview()
            make.right.equalTo(rightBtn.snp.left).offset(-10)
        }
        titleLabel.snp.makeConstraints { make in
            make.left.centerY.equalToSuperview()
        }
        lineView.snp.makeConstraints { make in
            make.centerX.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(4)
            make.height.equalTo(4)
            make.width.equalTo(titleLabel).offset(-12)
        }
        
        rightBtn.addTarget(self, action: #selector(rightBtnAction), for: .touchUpInside)
        lineView.isHidden = true
        
    }
    private lazy var backView: UIView = {
        let backView = UIView()
        return backView
    }()
    private lazy var titleBackView = UIView()
    private lazy var titleLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.font = kSetPingFangRegular(12)
        titleLabel.textColor = UIColor.hex(hexString: "868D9A")
        return titleLabel
    }()
    private lazy var lineView = UIView()
    private lazy var rightBtn: AppCustomButton = {
        let rightBtn = AppCustomButton()
        let marginTop = (SearchSectionHeaderCollectionReusableView.getSectionHeight()-18)/2
        rightBtn.imageEdgeInsets = UIEdgeInsets(top: marginTop, left: 16, bottom: marginTop, right: 16)
        return rightBtn
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
